package com.fh.service.impl;

import com.fh.mapper.ShoppingCartMapper;
import com.fh.model.ShoppingCart;
import com.fh.model.vo.ShoppingCartVO;
import com.fh.service.ShoppingCartService;
import com.fh.utils.ResultState;
import com.fh.vo.ResultVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartServiceImpl  implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    private SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
    @Override
    public ResultVo addCart(ShoppingCart cart) {
        cart.setCartTime(sdf.format(new Date()));
        int insert = shoppingCartMapper.insert(cart);

        if (insert>1){
            return new ResultVo(ResultState.OK,"成功",null);
        }else {
            return new ResultVo(ResultState.NO,"失败",null);
        }

    }

    @Override
    public ResultVo selectShoppingCart(Integer userId) {
        List<ShoppingCartVO> shoppingCartVOS = shoppingCartMapper.selectShoppingCart(userId);

          return new ResultVo(ResultState.OK,"成功",shoppingCartVOS);
    }

    @Override
    public ResultVo selectCartByCartId(String cartIds) {
        List<Integer> cartId=new ArrayList<>();
        String[] arr = cartIds.split(",");
        for (String s : arr) {
            cartId.add(Integer.parseInt(s));
        }
        List<ShoppingCartVO>   shoppingCartVOS= shoppingCartMapper.selectCartByCartId(cartId);


        return new ResultVo(ResultState.OK,"成功",shoppingCartVOS);
    }

    @Override
    public ResultVo updateNum(Integer cartId, String cartNum) {
      int i=shoppingCartMapper.updateNum(cartId,cartNum);

        return null;
    }


}
